class Session < ActiveRecord::Base
  
  attr_accessor :login, :password, :match # permet la lecture et l'�criture dans les attributs 
  belongs_to :account #cl� �trang�re - account_id
  before_validation :authenticate_account
  validates_presence_of :match,
    :message => 'for your login and password could not be found',
    :unless => :session_has_been_associated?
  before_save :associate_session_to_account
 
  private
 
  def authenticate_account
    unless session_has_been_associated?
      self.match = Account.find_by_login_and_password(self.login, self.password)
    end
  end
 
  def associate_session_to_account
    self.account_id ||= self.match.id
  end
 
  def session_has_been_associated?
    self.account_id
  end
  
end
